#ifndef __THREAD_HPP__
#define __THREAD_HPP__
#include <iostream>
#include <vector>
#include <pthread.h>
#include <unistd.h>
#include <functional>
#include <sys/syscall.h>

#define get_lwp_id() syscall(SYS_gettid);
using func_t = std::function<void()>;
const std::string threadnamedefault = "None - name";
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
class Thread
{
private:
    /* data */
    pthread_t _tid;
    pid_t lwpid;
    std::string _name;
    func_t _func;
    bool _isrunning;

public:
    Thread(func_t func, const std::string &name = threadnamedefault) : _name(name), _func(func), _isrunning(false)
    {
        std::cout << "create thread success" << std::endl;
    }
    ~Thread()
    {
    }
    static void *start_routine(void *args) // 取消this指针
    {
        Thread *self = static_cast<Thread *>(args);
        self->_isrunning = true;
        self->lwpid = get_lwp_id();
        self->_func();
        pthread_exit((void *)0);
    }
    void Start()
    {
        int n = pthread_create(&_tid, nullptr, start_routine, this);
        if (n == 0)
        {
            std::cout << "run thread success" << std::endl;
        }
    }
    void Join()
    {
        if (!_isrunning)
            return;
        int n = pthread_join(_tid, nullptr);
        if (n == 0)
        {
        }
    }
};

#endif